var User = require('../../login/lib/userSchema');
var consts = require('../../../util/consts');
var utils = require('../../../util/utils');
var moment = require('moment');

module.exports = function (app) {
    return new Handler();
};

var Handler = function (app) {
    this.app = app;
};

var handler = Handler.prototype;

handler.getInventList = function (msg, session, next) {

    var pageNum = msg.pageNum || 1;
    var pageSize = msg.pageSize || 10;
    pageSize = parseInt(pageSize);
    var type = msg.type;
    var uid = session.get('uid');
    let result;

    if (uid === null) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.USER_NOT_FOUND
        });
    }

    if (type === 'apprentice') {
        // 徒弟
        // console.log('徒弟 ' + type);
        User.find({parent_id: uid, is_effect_user: true}, {
            uid: 1,
            nickname: 1,
            gmt_create: 1,
            is_effect_user: 1,
            current_level: 1,
            parent_id: 1,
            video_num: 1,
            avatar: 1,
        }, function (err, users) {
            if (err) {
                next(null, {
                    code: consts.Code.ERR,
                    msg: consts.Msg.QUERY_FAILED
                });
                return;
            }
            let list = [];
            for (let i = 0; i < users.length; i++) {
                list.push({
                    uid: users[i].uid,
                    nickname: users[i].nickname,
                    parent_id: users[i].parent_id,
                    is_effect_user: users[i].is_effect_user,
                    current_level: users[i].current_level,
                    create_time: moment(users[i].create_time).format('YYYY-MM-DD HH:mm'),
                    is_help: users[i].video_num === 20,
                    video_num: users[i].video_num,
                    avatar: users[i].avatar,
                    limit_video_num: consts.Constant.DAILY_VIDEO_TIME,
                    max_help: consts.Constant.APPRENTICE_FULL_HELP_NUM,
                    min_help: consts.Constant.APPRENTICE_UN_FULL_HELP_NUM,
                });
            }
            next(null, {
                code: consts.Code.SUC,
                users: list
            });
        }).skip((pageNum - 1) * pageSize).limit(pageSize);
    } else if (type === 'disciple') {
        // 徒孙
        // console.log('徒孙 ' + type);
        result = [];
        User.aggregate([{$match: {parent_id: uid}}, {
            $lookup: {
                from: "users",
                localField: "uid",
                foreignField: "parent_id",
                as: "child",
            }
        }, {
            $unwind: '$child'
        }, {
            $project: {
                '_id': 0,
                'uid': "$child.uid",
                'parent_id': "$child.parent_id",
                'nickname': "$child.nickname",
                'create_time': "$child.create_time",
                'is_effect_user': "$child.is_effect_user",
                'current_level': "$child.current_level",
                'avatar': "$child.avatar",
            }
        }, {
            $match: {
                'is_effect_user': true,
            }
        }
        ], function (err, users) {
            let list = [];
            for (let i = 0; i < users.length; i++) {
                list.push({
                    uid: users[i].uid,
                    nickname: users[i].nickname,
                    parent_id: users[i].parent_id,
                    is_effect_user: users[i].is_effect_user,
                    current_level: users[i].current_level,
                    create_time: moment(users[i].create_time).format('YYYY-MM-DD HH:mm'),
                    avatar: users[i].avatar,
                });
            }
            next(null, {
                code: consts.Code.SUC,
                users: list
            });
        }).skip((pageNum - 1) * pageSize).limit(pageSize);
    } else if (type === 'not_active') {
        // 未激活
        result = [];
        User.aggregate([{$match: {parent_id: uid, is_effect_user: false}}, {
            $lookup: {
                from: "users",
                localField: "uid",
                foreignField: "parent_id",
                as: "child",
            }
        }, {
            $project: {
                '_id': 0,
                'uid': 1,
                'parent_id': 1,
                'nickname': 1,
                'create_time': 1,
                'is_effect_user': 1,
                'current_level': 1,
                'apprentice_num': 1,
                'avatar': 1,
                'child': 1,
            }
        }
        ], function (err, users) {
            for (var i = 0; i < users.length; i++) {
                if (users[i].is_effect_user === false) {
                    result.push(users[i]);
                }
                for (var j = 0; j < users[i].child.length; j++) {
                    delete users[i].child[j].fx_level;
                    delete users[i].child[j].__v;
                    delete users[i].child[j].gmt_modified;
                    delete users[i].child[j].gmt_create;
                    delete users[i].child[j].luck_draw_num;
                    delete users[i].child[j].video_num;
                    delete users[i].child[j].phone;
                    delete users[i].child[j].ip;
                    delete users[i].child[j].reward_jewel;
                    delete users[i].child[j].superior_treasure_num;
                    delete users[i].child[j].ordinary_treasure_num;
                    delete users[i].child[j].red_packet_income;
                    delete users[i].child[j].red_packet_level;
                    delete users[i].child[j].first_withdraw;
                    delete users[i].child[j].disciple_num;
                    // delete users[i].child[j].apprentice_num;
                    delete users[i].child[j].last_logout_time;
                    delete users[i].child[j].last_login_time;
                    delete users[i].child[j].coins;
                    delete users[i].child[j].jewel;
                    delete users[i].child[j].daily_profit;
                    delete users[i].child[j].yesterday_income;
                    delete users[i].child[j].total_income;
                    delete users[i].child[j].balance;
                    delete users[i].child[j].province;
                    delete users[i].child[j].city;
                    delete users[i].child[j].gender;
                    // delete users[i].child[j].avatar;
                    delete users[i].child[j].union_id;
                    delete users[i].child[j].title_logo;
                    delete users[i].child[j].title;
                    delete users[i].child[j]._id;
                    delete users[i].child[j].invite_id;
                    if (users[i].child[j].is_effect_user === false) {
                        result.push(users[i].child[j]);
                    }
                }
            }
            for (var k = 0; k < result.length; k++) {
                if (result[k].child !== null) {
                    delete result[k].child;
                }
            }
            let list = [];
            for (let i = 0; i < users.length; i++) {
                console.log("current_level = " + users[i].current_level);
                console.log("apprentice_num = " + users[i].apprentice_num);
                var info = "";
                if(users[i].current_level < utils.consts.FLOOR && users[i].apprentice_num < utils.consts.APPRENTICE){
                    info = "徒弟数不满" + utils.consts.APPRENTICE + "人，" + "矿层数不够" +utils.consts.FLOOR+ "层";
                } else if(users[i].apprentice_num < utils.consts.APPRENTICE){
                    info = "徒弟数不满" + utils.consts.APPRENTICE + "人";
                }else if(users[i].current_level < utils.consts.FLOOR){
                    info = "矿层数不够" +utils.consts.FLOOR+ "层";
                }
                list.push({
                    uid: users[i].uid,
                    nickname: users[i].nickname,
                    parent_id: users[i].parent_id,
                    is_effect_user: users[i].is_effect_user,
                    current_level: users[i].current_level,
                    create_time: moment(users[i].create_time).format('YYYY-MM-DD HH:mm'),
                    info:info,
                    avatar: users[i].avatar,
                });
            }
            next(null, {
                code: consts.Code.SUC,
                users: list
            });
        }).skip((pageNum - 1) * pageSize).limit(pageSize);
    } else {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.MISSING_PARAMETER
        });
    }
};
